/*!
 * 文件名称： main.c
 * 描    述： EXMC-外部SRAM读写实验主函数文件
 * 版本：     2023-12-03, V1.0
*/

/*
* GD32F303东方红开发板V1.0
* 淘宝商城：   https://juwo.taobao.com
* 技术交流：   https://www.gd32bbs.com/ask/
* 视频学习：   https://space.bilibili.com/475462605
* 微信公众号： gd32bbs
* Copyright    苏州聚沃电子科技有限公司
* 版权所有，盗版必究。
*/

#include "gd32f30x.h"
#include <stdio.h>
#include "bsp_led.h"
#include "bsp_uart.h"
#include "driver_public.h"
#include "driver_exmc.h"
#define EXMC_SRAM_SIZE             (512*1024/4)                  /*!< write or read buffer size */
/*!
* 说明     main函数
* 输入     无
* 输出     无
* 返回值   无
*/
int main(void)
{    
    uint32_t writereadstatus = 0;     
    driver_init();    																			 /* 延时和公共驱动部分初始化 */
    bsp_uart_init(&BOARD_UART);  														 /* 打印串口初始化 */  
    bsp_led_group_init();    																 /* 初始化LED组 */    
    bsp_led_off(&LED0);
    bsp_led_off(&LED1); 
    driver_exmc_norsram_init(EXMC_BANK0_NORSRAM_REGION0);    /* 初始化exmc norsram region0 */
    delay_ms(100);
    printf_log("External sram read and write examples.\r\n");    
  
    /* 以32位读写校验 */    
    printf_log("\r\n32-bit read/write check.\r\n"); 
    writereadstatus=0;    
    for(uint32_t index = 0; index < EXMC_SRAM_SIZE; index++ ){
        REG32(EXMC_BANK0_NORSRAM_REGION0_ADDR+index*4)=0xa55aa55a;
    }
    for(uint32_t index = 0; index < EXMC_SRAM_SIZE; index++ ){
        if(0xa55aa55a!=REG32(EXMC_BANK0_NORSRAM_REGION0_ADDR+index*4)){
            writereadstatus++;
            break;
        }
    }
    if(writereadstatus){
        bsp_led_on(&LED0);
        printf_log("32-bit read/write SRAM test failed!\r\n");
    }else{
        bsp_led_on(&LED1);
        printf_log("32-bit read/write SRAM test successed!\r\n");
    } 
    
   /* 以16位读写校验 */
    printf_log("\r\n16-bit read/write check.\r\n"); 
    writereadstatus=0;    
    for(uint32_t index = 0; index < EXMC_SRAM_SIZE; index++ ){
        REG16(EXMC_BANK0_NORSRAM_REGION0_ADDR+index*2)=0xaaaa;
    }
    for(uint32_t index = 0; index < EXMC_SRAM_SIZE; index++ ){
        if(0xaaaa!=REG16(EXMC_BANK0_NORSRAM_REGION0_ADDR+index*2)){
            writereadstatus++;
            break;
        }
    }
    if(writereadstatus){
        bsp_led_on(&LED0);
        printf_log("16-bit read/write SRAM test failed!\r\n");
    }else{
        bsp_led_on(&LED1);
        printf_log("16-bit read/write SRAM test successed!\r\n");
    } 
    /* 以8位读写校验 */
    printf_log("\r\n8-bit read/write check.\r\n"); 
    writereadstatus=0;    
    for(uint32_t index = 0; index < EXMC_SRAM_SIZE; index++ ){
        REG8(EXMC_BANK0_NORSRAM_REGION0_ADDR+index)=0x55;
    }
    for(uint32_t index = 0; index < EXMC_SRAM_SIZE; index++ ){
        if(0x55!=REG8(EXMC_BANK0_NORSRAM_REGION0_ADDR+index)){
            writereadstatus++;
            break;
        }
    }
    if(writereadstatus){
        bsp_led_on(&LED0);
        printf_log("8-bit read/write SRAM test failed!\r\n");
    }else{
        bsp_led_on(&LED1);
        printf_log("8-bit read/write SRAM test successed!\r\n");
    }     
	while (1)
	{  
	}
}

